package com.tommy.ddp.pipeline
{
	import com.tommy.ddp.GridDataManager;
	import com.tommy.ddp.MainManager;
	import com.tommy.ddp.box.DdpBox;
	import com.tommy.ddp.definition.DDPConfig;
	import com.tommy.ddp.definition.GridSkinFactory;
	import com.tommy.ddp.grid.DdpGrid;
	import com.tommy.ddp.manager.PipelineDataManager;
	import com.tommy.ddp.road.RoadFinder;
	import com.tommy.ddp.road.RoadWalker;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.utils.setTimeout;
	
	import org.taomee.component.containers.Box;
	
	import spark.components.supportClasses.Skin;

	/**
	 * 每一列的管道，管道可能会分布排列在不同的地方，会有连接
	 * @author Administrator
	 * 
	 */	
	public class Pipeline extends EventDispatcher
	{
		public static const MOVE_OVER:String = "moveOver";
		
		
		/**
		 * 与此管道关联、相通的管道(当某个管道直线上被阻挡的时候，检查左右是否可以行走，并关联上)
		 */		
		//private var crossPipeline:Vector.<Pipeline>;
		/**
		 * 相连接的管道 —— 一对一、单向
		 */		
		//private var connectPipeline:Pipeline;
		private var _column:uint;
		
		private var _roadVect:Vector.<PipelineRoadVo>;
		//其实可以删掉
		private var _newBoxVect:Vector.<DdpBox>;
		
		private var walkNum:uint;
		private var finishWalkNum:uint;
		
		
		
		public function Pipeline()
		{
		}
		public function setup(column:uint):void
		{
			_roadVect = new Vector.<PipelineRoadVo>();
			
			_column = column;
			
			for(var i:uint = 0;i<DDPConfig.H_NUM;i++)
			{
				var grid:DdpGrid = GridDataManager.getGrid(column,i);
				var roadVo:PipelineRoadVo = new PipelineRoadVo();
				roadVo.column = grid.column;
				roadVo.row = grid.row;
				roadVo.isEmpty = grid.isAvaliable;
				_roadVect.push(roadVo);
			}
			//
			checkConnet();
		}
		
		private function checkConnet():void
		{
			/*for(var i:uint = 0;i<DDPConfig.H_NUM;i++)
			{
				var grid:DdpGrid = GridDataManager.getGrid(_column,i);
				var nextGird:DdpGrid = GridDataManager.getGrid(_column,i+1);
			}*/
			for(var i:uint = 0;i<_roadVect.length;i++)
			{
				var grid:DdpGrid = GridDataManager.getGrid(_column,i);
				if(grid.skin.type == GridSkinFactory.EMPTY)
				{
					var prevGrid:DdpGrid = GridDataManager.getGrid(_column,i-1);
					
					var n:uint = i+1;
					var nextGrid:DdpGrid = GridDataManager.getGrid(_column,n);
					while((nextGrid && nextGrid.skin.type == GridSkinFactory.EMPTY) && n < _roadVect.length-1)
					{
						n++;
						nextGrid = GridDataManager.getGrid(_column,n);
					}
					if(nextGrid)
					{
						prevGrid.connectGrid = nextGrid;
						prevGrid.skin.mc["color"] = nextGrid.skin.mc["color"] = 0xff0000;
					}
					i = n;
				}
			}
		}
		
		/**
		 * 获得管道中指定位置的BOX
		 * @param i
		 * @return 
		 * 
		 */		
		public function getBox(i:uint):DdpBox
		{
			var vo:PipelineRoadVo = _roadVect[i];
			var box:DdpBox = GridDataManager.getBox(vo.column,vo.row);
			return box;
		}
		
		/**
		 * 上方有障碍格子的棋子队列
		 * @return 
		 * 
		 */		
		/*public function get blockBoxesList():Vector.<BlockBoxesVo>
		{
			var voList:Vector.<BlockBoxesVo> = new Vector.<BlockBoxesVo>();
			
			_blockBoxes = new Vector.<DdpBox>();
			
			var rowList:Array = [];
			for(var i:uint = 0;i<DDPConfig.H_NUM;i++)
			{
				var grid:DdpGrid = GridDataManager.getGrid(_column,i);
				if(!grid.isAvaliable)
				{
					grid = GridDataManager.getGrid(_column,i+1);
					if(grid && grid.isAvaliable)
						rowList.push(i+1);
				}
			}
			for(i = 0;i<rowList.length;i++)
			{
				var vo:BlockBoxesVo = new BlockBoxesVo();
				vo.row = rowList[i];
				var start:uint = rowList[i];
				var end:uint;
				if(i == rowList.length - 1)
					end = DDPConfig.H_NUM;
				else
					end = rowList[i+1];
				//
				var vect:Vector.<DdpBox> = new Vector.<DdpBox>();
				for(var j:uint = start;j<end;j++)
				{
					var box:DdpBox = GridDataManager.getBox(_column,j);
					if(box)
					{
						vect.push(box);
						_blockBoxes.push(box);
					}
				}
				vect.reverse();
				vo.boxes = vect;
				
				voList.push(vo);
			}
			//trace(rowList)
			
			return voList;
		}*/
		
		/**
		 * 填充路线上可用的格子（有可能一部分被障碍物隔离了）
		 * 
		 */		
		public function fillUpGridOfRoad():void
		{
			/*_newBoxVect = new Vector.<DdpBox>();
			
			var num:int = getCreateNum();
			for(var i:uint = 0;i<num;i++)
			{
				var newBox:DdpBox = new DdpBox();
				newBox.column = _column;
				newBox.row = -i-1;
				newBox.setType(Math.floor(Math.random()*DDPConfig.COLOR_NUM));
				MainManager.game.container.addChild(newBox);
				newBox.findRoad();
				
				_newBoxVect.push(newBox);
			}*/
			
			_newBoxVect = new Vector.<DdpBox>();
			var box:DdpBox = createNewBox();
			box.findRoad();
			while(box.roadFinder.path.length > 1)
			{
				_newBoxVect.push(box);
				MainManager.game.container.addChild(box);
				box.row = -_newBoxVect.indexOf(box)-1;
				//
				box = createNewBox();
				box.findRoad();
			}
			
		}
		private function createNewBox():DdpBox
		{
			var newBox:DdpBox = new DdpBox();
			newBox.column = _column;
			newBox.row = -1;
			newBox.setType(Math.floor(Math.random()*DDPConfig.COLOR_NUM));
			return newBox;
		}
		
		/*private function getCreateNum():int
		{
			var count:uint;
			for each(var i:PipelineRoadVo in _roadVect)
			{
				var grid:DdpGrid = GridDataManager.getGrid(i.column,i.row);
				if(i.isEmpty && !grid.isOccupied)
					count++;
				if(_column == 0)
				{
					trace(i.column,i.row,"isEmpty:",i.isEmpty,"isOccupied:",grid.isOccupied)
				}
			}
			return count;
//			var boxNum:uint;
//			for each(i in _roadVect)
//			{
//				var box:DdpBox = GridDataManager.getBox(i.column,i.row);
//				if(box)
//				{
//					var p:Point = box.finalPoint;
//					if(p.x == this._column)
//						boxNum++;
//				}
//			}
//			return count - boxNum;
		}
		*/
		
		public function move():void
		{
			finishWalkNum = 0;
			walkNum = 0;
			
			for each(var i:PipelineRoadVo in _roadVect)
			{
				var box:DdpBox = GridDataManager.getBox(i.column,i.row);
				if(box)
				{
					walkNum++;
					box.roadWalker.addEventListener(RoadWalker.FINISH,onFinish);
					box.walk();
				}
			}
			for each(var b:DdpBox in _newBoxVect)
			{
				walkNum++;
				b.roadWalker.addEventListener(RoadWalker.FINISH,onFinish);
				b.walk();
			}
		}
		
		private function onFinish(event:Event):void
		{
			var w:RoadWalker = event.currentTarget as RoadWalker;
			w.removeEventListener(RoadWalker.FINISH,onFinish);
			
			finishWalkNum++;
			if(finishWalkNum == walkNum)
			{
				dispatchEvent(new Event(MOVE_OVER));
			}
		}
		
		
		public function dispose():void
		{
			//crossPipeline = null;
			//connectPipeline = null;
			_roadVect = null;
			_newBoxVect = null;
		}
	}
}